package com.codi.bus.core.service.impl;

import com.codi.bus.constant.GlobalConstant;
import com.codi.bus.core.dao.TradeOrderDao;
import com.codi.bus.core.domain.TradeOrder;
import com.codi.bus.core.domain.UserBank;
import com.codi.bus.core.service.BankService;
import com.codi.bus.core.service.TradeOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 模块名
 *
 * @author spy
 * @version 1.0 2017-12-05 11:17
 * @since 1.0
 */
@Slf4j
@Service
@Transactional
public class TradeOrderServiceImpl implements TradeOrderService {

    @Autowired
    private BankService bankService;

    @Autowired
    private TradeOrderDao tradeOrderDao;


    @Override
    @Transactional(readOnly = true)
    public BigDecimal getCurrentDayDealSum(String tradeAccount) {
        return getDayDealSum(tradeAccount, new Date());
    }

    @Override
    @Transactional(readOnly = true)
    public BigDecimal getCurrentMonthDealSum(String tradeAccount) {
        return getMonthDealSum(tradeAccount, new Date());
    }

    @Override
    @Transactional(readOnly = true)
    public BigDecimal getDayDealSum(String tradeAccount, Date date) {
        UserBank bank = bankService.getByTradeAccount(tradeAccount);
        if (bank == null) {
            log.warn("未找到对应银行卡号");
            return BigDecimal.ZERO;
        }

        TradeOrder model = new TradeOrder();

        model.setUserId(bank.getUserId());
        model.setApplyDate(date);
        model.setTradeType(GlobalConstant.TRADE_TYPE_BUY);
        model.setOrderStatus(GlobalConstant.ORDER_STATUS_APPLY); //TODO 撤单
        model.setBankCode(bank.getBankCode());
        model.setPaymentType(bank.getCapitalMode());

        return tradeOrderDao.getSumByDay(model);
    }

    @Override
    @Transactional(readOnly = true)
    public BigDecimal getMonthDealSum(String tradeAccount, Date date) {
        UserBank bank = bankService.getByTradeAccount(tradeAccount);
        if (bank == null) {
            log.warn("未找到对应银行卡号");
            return BigDecimal.ZERO;
        }

        TradeOrder model = new TradeOrder();

        model.setUserId(bank.getUserId());
        model.setApplyDate(date);
        model.setTradeType(GlobalConstant.TRADE_TYPE_BUY);
        model.setOrderStatus(GlobalConstant.ORDER_STATUS_APPLY); //TODO 撤单
        model.setBankCode(bank.getBankCode());
        model.setPaymentType(bank.getCapitalMode());

        return tradeOrderDao.getSumByDay(model);
    }

    @Override
    public List<Date> getConfirmDates(String fundCode, Long userId) {
        return tradeOrderDao.getConfirmDates(fundCode.trim(), userId);
    }

    @Override
    public List<Date> getSellConfirmDates(String fundCode, Long userId) {
        return tradeOrderDao.getConfirmSellDates(fundCode.trim(), userId);
    }
}
